<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/normal',
	 * meaning using a single column layout. See 'protected/views/layouts/normal.php'.
	 */
	public $layout='//layouts/normal';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

	public $baseUrl = "";
	public $curr_user = null;

	public function init(){
		parent::init();
		if(is_object(Yii::app()->user->getState('current_user'))){
			$this->curr_user = Yii::app()->user->getState('current_user');
		}

		//$this->baseUrl = Yii::app()->baseUrl;
		//host
		if($_SERVER['SERVER_PORT'] == 80) {
			$host = 'http://'.$_SERVER['HTTP_HOST'];
		} else {
			$host = 'https://'.$_SERVER['HTTP_HOST'];
		}
		$seft = $_SERVER['PHP_SELF'];
		$expl = explode('/', $seft);
		if(count($expl) > 2) {
			$s1 = '';
			foreach($expl as $ii=>$it) {
				if(($ii + 1) < count($expl)) {
					if($s1 != "")
						$s1.='/';
					$s1.=$it;
				}
			}
			$host.='/'.$s1;
		}
		$this->baseUrl = $host;

		// JS global variables
		// Use:  yii.baseUrl
		Yii::app()->clientScript->registerScript('helpers', '
			yii = {
				baseUrl: '.CJSON::encode(Yii::app()->baseUrl).'
			};
		',CClientScript::POS_HEAD);
		Yii::app()->clientScript->registerCoreScript('jquery', CClientScript::POS_HEAD);
	}

	/**
	 * Performs the AJAX validation.
	 * @param User $model the model to be validated
	 */
	public function validateAjax($model)
	{
		if(isset($_POST['ajax']))
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}